package com.ice.body.active.service.impl;

import com.ice.base.dao.BaseDao;
import com.ice.base.service.impl.BaseServiceImpl;
import com.ice.base.util.DateUtils;
import com.ice.body.active.dao.ActiveChildMapper;
import com.ice.body.active.dao.ActiveMainMapper;
import com.ice.body.active.model.ActiveChildModel;
import com.ice.body.active.model.ActiveMainModel;
import com.ice.body.active.service.ActiveMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created with GuoYanBin.
 * Description:激活主表 serviceImpl
 * Date: 2022-10-18
 * Time: 11:47
 */
@Service
public class ActiveMainServiceImpl extends BaseServiceImpl<ActiveMainModel> implements ActiveMainService{
    @Resource 
    private ActiveMainMapper activeMainMapper;
    @Resource
    private ActiveChildMapper activeChildMapper;
    @Override
    public BaseDao<ActiveMainModel> getBaseDao() {
        return activeMainMapper;
    }

    /**
     * 校验是否是激活的产品
     * @return
     */
    @Override
    public Integer validateActive() {
        int flag = 0;
        ActiveMainModel active = activeMainMapper.selectById("1");
        Date actTime = active.getActTime();
        //激活时间小于当前时间为过期 并且激活标志为非激活 系统不可用
        if(actTime.before(new Date()) && active.getActFlag() != 1){
            return flag;
        }
        return 1;
    }

    //
    @Transactional
    @Override
    public Integer active(String actCode) {
        int flag = 1;

        ActiveChildModel child = activeMainMapper.selectByActCode(actCode);
        if(null != child && child.getUseFlag() == 0){
            //满足条件为可用
            int longFlag = child.getLongFlag();
            child.setUseFlag(1);
            child.setActTime(new Date());
            activeChildMapper.update(child);
            ActiveMainModel activeMainModel = activeMainMapper.selectById("1");
            if(longFlag == 1 ){
                activeMainModel.setActFlag(1);
            }else{
                activeMainModel.setActTime(DateUtils.subMonth(new Date()));
            }
            activeMainMapper.update(activeMainModel);
        }else{
            flag = 0 ;
        }

        return flag;
    }
}
